#region Using

using System;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CoyoEden;
using System.IO;
using System.Drawing;
using CoyoEden.Web.Controls;
using CoyoEden.Models;
#endregion

public partial class admin_Pages_Categories : AdminBasePage
{
	/// <summary>
	/// Handles the Load event of the Page control.
	/// </summary>
	/// <param name="sender">The source of the event.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			LoadParentDropDown(ddlNewParent);

			//add some js and css
			Utils.AddJavaScriptInclude(Utils.RelativeWebRoot + "assets/js/plugins/flexigrid.js", Page, true);
			Utils.AddStylesheetInclude(Utils.RelativeWebRoot + "assets/css/flexigrid.css", Page, false);
		}
		Page.Title = Resources.labels.categories;
	}

	#region helper methods
	private void LoadParentDropDown(DropDownList ddl)
	{
		// Load up the Parent DropDown
		ddl.ClearSelection();
		ddl.Items.Add(new ListItem("none", "-1"));
		foreach (Category cat in Category.Categories)
		{
			ddl.Items.Add(new ListItem(cat.CompleteTitle(), cat.Id.ToString()));
		}
	}
	protected string GetParentTitle(object item)
	{
		Category temp = (Category)item;
		if (temp.Parent<0)
			return "";
		else
			return Category.GetCategory(temp.Parent).Title;
	}
	/*
	private void AddNew() {
		Category cat = new Category(txtNewCategory.Text, txtNewNewDescription.Text);
		if (ddlNewParent.SelectedValue != "0")
			cat.Parent = Convert.ToInt32(ddlNewParent.SelectedValue);
		//slug
		cat.Slug = txtNCSlug.Text.Trim();
		//icon
		cat.Icon = this.imgNCIcon.ImageUrl;
		cat.Save();
		Response.Redirect(Request.RawUrl, true);
	}
	private void Update() {
		this.selectedCategory.Title = txtNewCategory.Text.Trim();
		this.selectedCategory.Description = txtNewNewDescription.Text.Trim();
		if (this.ddlNewParent.SelectedValue != "0") {
			this.selectedCategory.Parent =Convert.ToInt32(this.ddlNewParent.SelectedValue);
		}
		this.selectedCategory.Slug = this.txtNCSlug.Text.Trim();
		this.selectedCategory.Icon = this.imgNCIcon.ImageUrl;
		this.selectedCategory.Save();
		Response.Redirect(Request.RawUrl);
	}
	

	void btnUploadImage_Click(object sender, EventArgs e)
	{
		string fileName=this.txtUploadImage.FileName;
		//default imgsize
		int imgSize = 80;
		using (System.Drawing.Image rawImg = System.Drawing.Image.FromStream(this.txtUploadImage.PostedFile.InputStream))
		{
			Bitmap img = Utils.GenAThumbnail(this.txtUploadImage.PostedFile, imgSize, imgSize);
			Utils.Upload(Page.Server, this.categoryIconRelativeUrl, img, rawImg.RawFormat, fileName);
			img.Dispose();
		}
		//We should use ImageHanlder(image.axd) here to access the App_Data folder.
		string path = string.Format("{0}image.axd?picture={1}",Utils.AbsoluteWebRoot.ToString(),this.categoryIconRelativeFolder+fileName);
		this.imgNCIcon.ImageUrl = path;
	}
	 */
	#endregion
}
